package com.jiazhong.schedule.controller;

import com.alibaba.fastjson.JSON;
import com.jiazhong.schedule.pojo.SysSchedule;
import com.jiazhong.schedule.service.SysScheduleService;
import com.jiazhong.schedule.service.impl.SysScheduleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/queryAll")
public class ScheduleController extends HttpServlet {
    private SysScheduleService service = new SysScheduleServiceImpl();

/*        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            List<SysSchedule> sysSchedules = service.queryAll();
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();

            //渲染到页面当中
            out.write(JSON.toJSONString(sysSchedules));
            resp.getWriter().write(JSON.toJSONString(sysSchedules));
            out.flush();//刷新,若果没有刷新,数据不会写入到页面中
           out.close();//关闭流
        }*/
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<SysSchedule> sysSchedules = service.queryAll();
        resp.setContentType("application/json;charset=utf-8");

        // 只需要写入一次即可
        resp.getWriter().write(JSON.toJSONString(sysSchedules));
    }
}
